﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using System.Threading;

namespace csTempWindowsApplication1
{
    public class Form1 : Form
    {
        // Constant value was found in the "windows.h" header file.
        private const int WM_ACTIVATEAPP = 0x001C;
        private TextBox textBoxMessages;
        private bool appActive = true;
        private IntPtr id;

        [STAThread]
        static void Main()
        {            

            
            Application.Run(new Form1());
        }

        public Form1()
        {           
            id = this.Handle;
            this.Text = "Form1";
            //this.Size = new System.Drawing.Size(300, 300);
            //this.Text = "Form1";
            //this.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            InitializeComponent();
        }
        /*
        protected override void OnPaint(PaintEventArgs e)
        {
            // Paint a string in different styles depending on whether the
            // application is active.
            if (appActive)
            {

                e.Graphics.FillRectangle(SystemBrushes.ActiveCaption, 20, 20, 260, 50);
                e.Graphics.DrawString("Application is active", this.Font, SystemBrushes.ActiveCaptionText, 20, 20);
            }
            else
            {
                e.Graphics.FillRectangle(SystemBrushes.InactiveCaption, 20, 20, 260, 50);
                e.Graphics.DrawString("Application is Inactive", this.Font, SystemBrushes.ActiveCaptionText, 20, 20);
            }
        }*/

        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void WndProc(ref Message m)
        {
            // Listen for operating system messages.
            switch (m.Msg)
            {
                // The WM_ACTIVATEAPP message occurs when the application
                // becomes the active application or becomes inactive.
                case WM_ACTIVATEAPP:

                    // The WParam value identifies what is occurring.
                    appActive = (((int)m.WParam != 0));
                    
                    int hwnd = DllLoader.GetForegroundWindow();        
                    
                    AddText(hwnd.ToString() + "__" + id.ToString()+"__"+m.HWnd+"\r\n");
                    if (hwnd != id.ToInt32() && hwnd != 0)
                    {
                        StringBuilder builder = new StringBuilder(128);
                        DllLoader.GetClassName(hwnd, builder, builder.Capacity);
                        AddText(builder.ToString());
                    }

                    /*
                    if(appActive == false){
                        int hwnd = DllLoader.GetForegroundWindow();
                        StringBuilder builder = new StringBuilder(128);
                        DllLoader.GetClassName(hwnd, builder, builder.Capacity);
                        AddText(builder.ToString());
                        //MessageBox.Show(builder.ToString());                        
                    }
                     */ 


                    // Invalidate to get new text painted.
                    this.Invalidate();

                    break;
            }
            base.WndProc(ref m);
        }

        private void AddText(string message)
        {
            if (message == null)
            {
                return;
            }

            int length = textBoxMessages.Text.Length + message.Length;
            if (length >= textBoxMessages.MaxLength)
            {
                textBoxMessages.Text = "";
            }

            if (!message.EndsWith("\r\n"))
            {
                message += "\r\n";
            }

            textBoxMessages.Text = message + textBoxMessages.Text;
        }

        private void InitializeComponent()
        {
            this.textBoxMessages = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // textBoxMessages
            // 
            this.textBoxMessages.BackColor = System.Drawing.Color.White;
            this.textBoxMessages.Location = new System.Drawing.Point(12, 12);
            this.textBoxMessages.Multiline = true;
            this.textBoxMessages.Name = "textBoxMessages";
            this.textBoxMessages.ReadOnly = true;
            this.textBoxMessages.Size = new System.Drawing.Size(260, 86);
            this.textBoxMessages.TabIndex = 0;
            this.textBoxMessages.TabStop = false;
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.textBoxMessages);
            this.Name = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}